﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using Core;
using System.Data;

namespace DAL
{
    public class TinTucDAL
    {
        public static List<TinTuc> LayTatCa()
        {
            try
            {
                return CBO.FillCollection<TinTuc>(DataProvider.Instance.ExecuteReader("TinTuc_LayTatCa"));
            }
            catch
            {

                return new List<TinTuc>();
            }

        }

        public static List<TinTuc> SapGiam()
        {
            try
            {
                return CBO.FillCollection<TinTuc>(DataProvider.Instance.ExecuteReader("TinTuc_SapGiam"));
            }
            catch
            {

                return new List<TinTuc>();
            }

        }
        public static bool Them(TinTuc tt)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@IDTinTuc", "TinTuc_Them", tt.IDTinTuc, tt.TieuDe, tt.NoiDung, tt.HinhAnh, tt.NgayDang, tt.HienThi, tt.MoTa, tt.UserID);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {

                return false;
            }

        }
        public static bool Sua(TinTuc tt)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("TinTuc_Sua", tt.IDTinTuc, tt.TieuDe, tt.NoiDung, tt.HinhAnh, tt.NgayDang, tt.HienThi, tt.MoTa, tt.UserID);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {

                return false;
            }

        }
        public static bool Xoa(string Id)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("TinTuc_Xoa", Convert.ToInt32(Id));
                return rs > 0;
            }
            catch
            {

                return false;
            }

        }
        public static TinTuc LayID(string IdTinTuc)
        {
            try
            {
                return CBO.FillObject<TinTuc>(DataProvider.Instance.ExecuteReader("TinTuc_LayID", Convert.ToInt32(IdTinTuc)));
            }
            catch
            {
                return null;
            }
        }
        public static List<TinTuc> TTPhanTrang(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("TinTuc_PhanTrang", GlobalConfiguration.DescriptionLength, page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<TinTuc>(reader);

            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<TinTuc>();
            }
        }
        public static List<TinTuc> Paging(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader(
                     "TinTuc_Paging",
                     page,
                     GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) /
        (double)pageSize);

                reader.NextResult();
                return CBO.FillCollection<TinTuc>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<TinTuc>();
            }
        }
        public static bool CapNhatHienThi(string ID, bool hienthi)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery(
        "TinTuc_UpdateTinTuc", Convert.ToInt32(ID), hienthi);
                return rs > 0;
            }
            catch
            {
                return false;
            }
        }


        public static List<TinTuc> TimKiemNgauNhien(string tieuDe, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("TinTuc_TimKiemNgauNhien", tieuDe, GlobalConfiguration.DescriptionLength, page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<TinTuc>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<TinTuc>();
            }
        }

    }
}
